package ControlLayer;//makes this class a part of the ControlLayer package
import ModelLayer.*;
import java.util.*;

/**
 * 
 * Controller class for the AddressBook
 * <p>
 * Accepts input user data from the AddressUI(TUI Layer) and manages the data stored in AdressBook and Person(ModelLayer).
 * <br>
 * @author (dmaj0913 - group 2)
 * @version (2013.11.08)
 * <p>
 * <b><i>History:</i></b>
 * <p>
 * (KBH): 2005.03.31:
 * <p>
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering and loose coupling.
 * 
 */
public class AddressCtr
{
    private AddressBook book;
    
    /**
     * Constructor gets a backup of a last AddressBook with friends.  
     * @param backup stores a recently created new Addressbook or a backup of it that was used previously. 
     */
    public AddressCtr(AddressBook backup)
    {
        book = backup;
    }
    
    /**
     * Checks whether a friend exists in the System.
     * @param id stores the id of a friend.
     */
    public boolean isPerson(long id)
    {
        return book.getPerson(id)!=null;
    }
    /**
    * Finds a friend by his id, and if it should be printed, it prints out in the Terminal all the information about him.
    * @param id stores the id of the friend that is to be found.
    * @param print stores the statement wheather it should print the information about the friend or just return him.
    * @return object of type Person which contains all the information about the desired person.
    */
    public Person getPerson(long id, boolean print)
    {
        
        Person person=book.getPerson(id);
        if (person!=null)
        {
            if(print == true)  person.printInformation();
        }
        return person;
    }
    /**
     * Creates new friend and adds him to the collection.
     * @param id takes a number of type long and stores the id of the friend.
     * @param name stores a character string of the name of the friend.
     * @param address stores a character string of the address of the friend.
     * @param postalCode stores a character string of the Postal Code of the friend.
     * @param city stores a character string of the city of the friend.
     * @param phone stores a character string of the phone number of the friend.
     */
    public void createPerson(long id, String name, String address, String postalCode, String city, String phone)
    {   
       //creates a Person object and stores it in the container
       book.addPerson(new Person(id,name,address,postalCode,city,phone));
    }
    
    /**
     * Deletes a friend by using his id number.
     * @param id takes a number in the range of type long that is used as an desired id.
     */
    public void deletePerson(long id)
    {
        //search for the person and delete the object from the container
        Person person=book.getPerson(id);
        if (person!=null)
        {book.deletePerson(book.getPerson(id));
        System.out.println("Person with id "+id+" has been deleted.");}
        else System.out.println("That person does not exist");
    }
    
    /**
     * Updates an existing Friend, which is found by ID, by editing all his fields.
     * @param id takes a number of type long and stores the new id of the friend.
     * @param name stores a character string of the new name of the friend.
     * @param address stores a character string of the new address of the friend.
     * @param postal stores a character string of the new Postal Code of the friend.
     * @param city stores a character string of the new city of the friend.
     * @param tel stores a character string of the new phone number of the friend.
     */
    public void updatePerson(long id,String name, String address, String postal, String city, String tel)
    {
        Person person=book.getPerson(id);
        if (person !=null)
        {book.updatePerson(person,name,address,postal,city,tel);}
    }
    
    /**
     *Prints out in the Terminal all information about all existing friends.
     */
    public void listAllPersons()
    {
        ArrayList<Person> people=book.getPersons();
        for (Person person:people)
        {person.printInformation();}
    }
    /**
     * returns all the friends in the AddressBook
     * @return a variable of type AddressBook that contains all the people who are added to the system so far.
     */
    public AddressBook getAddressBook()
    {
        return book;
    }
}
